package encaps.utils.rendering;

import encaps.resources.Response;
import encaps.utils.ImageServlet;
import static encaps.utils.html.HTML.*;
import encaps.utils.html.HTMLElement;
import org.jdom.Element;

public class ImageButtonFactory implements FieldFactory {

    @Override
    public HTMLElement renderField(Element e, boolean edit) {
        HTMLElement imagebutton = new HTMLElement(BUTTON);
        String img = e.getChildTextTrim(Response.LABEL);
        String key = e.getChildTextTrim(Response.KEY);
        String value = e.getChildTextTrim(Response.VALUE);

        //imagebutton.addAttribute(TYPE, IMAGE);
        imagebutton.addAttribute(NAME, key);
        imagebutton.addAttribute(VALUE, value);

        HTMLElement image = new HTMLElement(IMG);
        image.addAttribute(SRC, String.format("%s?%s=%s",
                ImageServlet.IMAGE_SERVLET_NAME, ImageServlet.PATH, img));
        image.addAttribute(ALT, value);
        imagebutton.addContent(image);

        return imagebutton;
    }
}